/**
 * Created by zhd on 2017/6/23.
 */
var MapLayer = cc.Layer.extend({
    _drawNode: null,
    _route: [], // 路线
    _balls: [],

    ctor: function () {
        this._super();
        this.loadInit();
    },

    /**
     * 初始化
     */
    loadInit: function () {
        this._drawNode = new cc.DrawNode();
        this.addChild(this._drawNode);

        for (var i = 0; i < g_levelInfo.maps.length; i++) {
            this._drawNode.drawDot(g_levelInfo.maps[i], 10, cc.color(0, 0, 0, 100));
        }

        this.loadRoute();
        this.outBall();
        this.scheduleUpdate();
    },

    /**
     * 加载路径
     */
    loadRoute: function () {
        var radian = 0;// 弧度
        var rotation = 0;//度数
        var max_range = 0;// 距离

        for (var i = 0; i < g_levelInfo.maps.length - 1; i++) {
            var currentPos = g_levelInfo.maps[i];
            var nextPos = g_levelInfo.maps[i + 1];

            radian = Math.atan2(nextPos.x - currentPos.x, nextPos.y - currentPos.y);
            rotation = 180 / Math.PI * radian;
            rotation = rotation < 0 ? 360 + rotation : rotation;

            if (nextPos.x - currentPos.x != 0) {
                max_range = (nextPos.x - currentPos.x) / Math.sin(radian);
            } else {
                max_range = Math.abs(nextPos.y - currentPos.y);
            }

            for (var j = 0; j <= max_range; j++) {
                var x = g_levelInfo.maps[i].x + j * Math.sin(radian);
                var y = g_levelInfo.maps[i].y + j * Math.cos(radian);
                this._route.push({x: x, y: y, rotation: rotation});
            }
        }
    },

    /**
     * 出球
     */
    outBall: function () {
        var ball = new BallSprite(g_levelInfo.balls.shift());
        var route = this._route[0];
        ball.setPosition(cc.p(route.x, route.y));
        ball.rotation = route.rotation;
        this.addChild(ball);
        this._balls.push(ball);
    },

    /**
     * 像之前插入球
     * @param index 碰撞球下标
     * @param tag 颜色标识
     * @param pos 碰撞球位置
     */
    insertBeforeBall: function (index, tag, pos) {
        for (var i = 0; i < GC.balls.length; i++) {
            if (tag == GC.balls[i].tag) {
                break;
            }
        }
        var ball = new BallSprite(i);
        ball.setPosition(pos);
        ball._step = this._balls[index]._step + this._balls[index].width;
        ball._isRoll = this._balls[index]._isRoll;
        ball.homing();
        this.addChild(ball);

        this._balls.splice(index, 0, ball);

        for (var i = index - 1; i >= 0; i--) {
            this._balls[i].forward(this._balls[i].width);
        }
    },

    /**
     * 像之后插入球
     * @param index 碰撞球下标
     * @param tag 颜色标识
     * @param pos 碰撞球位置
     */
    insertAfterBall: function (index, tag, pos) {
        for (var i = 0; i < GC.balls.length; i++) {
            if (tag == GC.balls[i].tag) {
                break;
            }
        }
        var ball = new BallSprite(i);
        ball.setPosition(pos);
        ball._step = this._balls[index]._step;
        ball._isRoll = this._balls[index]._isRoll;
        ball.homing();
        this.addChild(ball);

        this._balls.splice(index + 1, 0, ball);

        for (var i = index; i >= 0; i--) {
            this._balls[i].forward(this._balls[i].width);
        }
    },

    /**
     * 停止所有球动作
     */
    stopBalls: function () {
        for (var i = 0; i < this._balls.length; i++) {
            this._balls[i].stopAction();
            this._balls[i].unscheduleAllCallbacks();
        }
    },

    update: function (dt) {
        // 出球检查
        if (g_levelInfo.balls.length &&
            this._balls[this._balls.length - 1]._step - this._balls[this._balls.length - 1].width == 0) {
            this.outBall();
        }

        // 球滚动
        for (var i = this._balls.length - 2; i >= 0; i--) {

            // 判断不运动的球
            if (!this._balls[i]._isRoll) {
                if (i < this._balls.length - 1 &&
                    this._balls[i + 1]._step + this._balls[i + 1].width >= this._balls[i]._step
                ) {
                    this._balls[i].roll();
                } else if (this._balls[i + 1]._tag == this._balls[i]._tag) {
                    
                }
                break;
            }
        }
    },

    /**
     * 检查相同的球
     */
    checkSame: function () {
        var clear_arr = [this._balls[0]];
        var index = 0;

        /**
         * 相同颜色球找不来
         */
        for (var i = 1; i < this._balls.length; i++) {
            if (clear_arr[clear_arr.length - 1]._tag == this._balls[i]._tag) {
                clear_arr.push(this._balls[i]);
            } else if (clear_arr.length >= 3) {
                break;
            } else {
                index = i;
                clear_arr = [this._balls[i]];
            }
        }

        /**
         * 清除相同颜色球
         */
        if (clear_arr.length >= 3) {
            /**
             * 停住相同颜色球后的球
             */
            if (this._balls.length - clear_arr.length > index) {
                for (var i = index - 1; i >= 0; i--) {
                    this._balls[i].stopRoll();
                }
            }

            /**
             * 消除球
             */
            this._balls.splice(index, clear_arr.length);
            for (var i = 0; i < clear_arr.length; i++) {
                this.removeChild(clear_arr[i]);
            }
        }

    }


})